// pages/order/amendTime/amendTime.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    go_right: '../../../images/go_right.png',
    work_order_id: '',//工单ID

    startDate: '',
    startTime: '',
    endDate: '',
    endTime: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    var id = options.id;
    that.setData({
      work_order_id: id
    });
    that.getOrderDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  // 获取订单详情
  getOrderDetail:function(){
    let that = this;
    app.ajax({
      url: 'Engineer/Workorder/workOrderDetail',
      data: { work_order_id: that.data.work_order_id},
      success: function(res){
        if(res.data.code == 1000){
          if (res.data.data.appointment_start_time){
            let start = res.data.data.appointment_start_time.split(" ");
            let end = res.data.data.appointment_end_time.split(" ");
            that.setData({
              startDate: start[0],
              startTime: start[1],
              endDate: end[0],
              endTime: end[1],
            });
          }
        }
      }
    })

  },
  //选择日期
  bindDateChange: function (e) {
    let that = this;
    let type = e.currentTarget.dataset.type;
    let date = e.detail.value;
    if(type==1){
      that.setData({
        startDate: date
      })
    }else{
      that.setData({
        endDate: date
      })
    }
  },
  //选择时间
  bindTimeChange: function (e) {
    let that = this;
    let type = e.currentTarget.dataset.type;
    let time = e.detail.value;
    if (type == 1) {
      that.setData({
        startTime: time
      })
    } else {
      that.setData({
        endTime: time
      })
    }
  },
  // 确认预约时间
  sureTime: function(e){
    let that = this;
    let id = that.data.work_order_id;
    let start = that.data.startDate + ' ' + that.data.startTime;
    let end = that.data.endDate + ' ' + that.data.endTime;
    if (start != '' && end!=''){
      wx.showModal({
        title: '提示',
        content: '是否确认预约时间为：\r\n' + start + '-' + end,
        success(res){
          if(res.confirm){
            app.ajax({
              url: 'Engineer/Workorder/editAppointmentTime',
              data: {
                work_order_id: id,
                appointment_start_time: start,
                appointment_end_time: end
              },
              success: function (res) {
                if (res.data.code == 1000) {
                  app.showToast("确认预约成功", "none", 2000, function () { });
                  wx.switchTab({
                    url: '../../order/myorder/myorder'
                  });
                  wx.setStorageSync('tabIndex', 1);

                } else {
                  app.showToast(res.data.message, "none", 2000, function () { });
                }
              }
            })
          }
          if(res.cancel){

          }
        }
      })
    }else{      
      app.showToast('请选择时间','none',2000,function(){})
    }
  }
})